package service

import (
	"api_interface/dao"
	"api_interface/entity"
)

/*
创建一个新提问箱(完成)
*/
func CreateBox(box *entity.BoxInfo) (err error) {
	if err = dao.SqlSession.Create(box).Error; err != nil {
		return err
	}
	return
}

/*
查找某人有没有加入过某提问箱(完成)
*/
func CheckHaveJoin(m *entity.MemberInfo) (mem *entity.MemberInfo, err error) {
	var member entity.MemberInfo
	if err = dao.SqlSession.Where("mid = ? AND bid = ?", m.Mid, m.Bid).First(&member).Error; err != nil {
		return nil, err
	}
	mem = &member
	return
}

/*
更新member_info加入信息，主要是更新管理员权限(完成)
*/
func UpdateMemberInfo(mem *entity.MemberInfo) (err error) {
	err = dao.SqlSession.Model(&entity.MemberInfo{}).Where("bid = ? AND mid = ?", mem.Bid, mem.Mid).Update("is_adm", mem.IsAdm).Error
	return
}

/*
更新box_info
*/
func UpdateQuestionBoxInfo(box *entity.BoxInfo) (err error) {
	err = dao.SqlSession.Model(&entity.BoxInfo{}).Where("id = ?", box.Id).Update(entity.BoxInfo{
		Nam:  box.Nam,
		Text: box.Text,
		Url:  box.Url,
	}).Error
	return
}

/*
在提问箱重删除一个成员(完成)
*/
func RemoveMemberOfBox(bid string, mid string) (err error) {
	err = dao.SqlSession.Where("bid=? AND mid=?", bid, mid).Delete(&entity.MemberInfo{}).Error
	return
}

/*
根据提问箱id删除对应提问箱(完成)
*/
func DeleteBoxById(id string) (err error) {
	err = dao.SqlSession.Where("id=?", id).Delete(&entity.BoxInfo{}).Error
	return
}

/*
提问箱有新成员加入，向memberinfo中插入一条信息(完成)
*/
func AccountJoinBox(mem *entity.MemberInfo) (err error) {
	if err = dao.SqlSession.Create(mem).Error; err != nil {
		return err
	}
	return
}

/*
根据所有者id来查找他的提问箱(完成)
*/
func GetUserCreateBoxByOid(id int) (box *[]entity.BoxInfo, err error) {
	var boxList []entity.BoxInfo
	if err = dao.SqlSession.Where("oid=?", id).Order("id DESC").Find(&boxList).Error; err != nil {

		return nil, err
	}
	box = &boxList
	return
}

/*
通过账户id获取他加入的MemberInfo信息(完成)
*/
func GetJoinedBoxById(mid int) (mem *[]entity.MemberInfo, err error) {
	var memList []entity.MemberInfo
	if err = dao.SqlSession.Where("mid = ? AND is_owner = ?", mid, false).Find(&memList).Error; err != nil {
		return nil, err
	}
	mem = &memList
	return
}

/*
通过账户id获取他管理的MemberInfo信息(完成)
*/
func GetManageBoxById(mid int) (mem *[]entity.MemberInfo, err error) {
	var memList []entity.MemberInfo
	if err = dao.SqlSession.Where("mid = ? AND is_adm = ?", mid, true).Find(&memList).Error; err != nil {
		return nil, err
	}
	mem = &memList
	return
}

/*
根据提问箱id得到提问箱详情信息(完成)
*/
func GetBoxById(id int) (box *entity.BoxInfo, err error) {
	var box_info entity.BoxInfo
	if err = dao.SqlSession.Where("id=?", id).First(&box_info).Error; err != nil {
		return nil, err
	}
	box = &box_info
	return
}

/*
获取提问箱的管理员所在的member_info(完成)
*/
func GetBoxOwner(bid int) (mem *entity.MemberInfo, err error) {
	var mem_info entity.MemberInfo
	if err = dao.SqlSession.Where("bid = ? AND is_owner=?", bid, true).Find(&mem_info).Error; err != nil {
		return nil, err
	}
	mem = &mem_info
	return
}

/*
通过账户id找到所有的账户信息(完成)
*/
func GetAccountById(id int) (acc *entity.AccountInfo, err error) {
	var account entity.AccountInfo
	if err = dao.SqlSession.Where("id = ? ", id).First(&account).Error; err != nil {
		return nil, err
	}
	acc = &account
	return
}

/*
通过提问箱id找到这个提问箱所有的memberinfo信息
*/
func GetBoxMemberById(bid int) (mem *[]entity.MemberInfo, err error) {
	var member_list []entity.MemberInfo
	if err = dao.SqlSession.Where("bid = ?", bid).Find(&member_list).Error; err != nil {
		return nil, err
	}
	mem = &member_list
	return
}

/*
检查用户是否已经加入该提问箱
*/
func Check_is_joined_box(mid, bid int) (mem *entity.MemberInfo, err error) {
	var mem_info entity.MemberInfo
	result := dao.SqlSession.Where("mid = ? AND bid = ?", mid, bid).Find(&mem_info)
	if result.Error != nil {
		return nil, result.Error
	}
	if result.RowsAffected == 0 { // 假设你的ORM库提供了RowsAffected来指示受影响的行数
		return nil, nil // 或者返回一个特定的错误，如 errors.New("not found")
	}
	return &mem_info, nil
}

func SearchManageBoxByTitle(title string) (box *[]entity.BoxInfo, err error) {
	var boxList []entity.BoxInfo
	if err = dao.SqlSession.Where("nam LIKE ?", "%"+title+"%").Find(&boxList).Error; err != nil {
		return nil, err
	}
	box = &boxList
	return
}

/*
根据提问箱id获取里面的提问(不归我管)
*/
/*
func GetQueById(bid string, page int, page_size int) (que []*entity.QuestionInfo, err error) {
	if err = dao.SqlSession.Where("bid = ?", bid).Offset(page * page_size).Limit(page_size).Find(&que).Error; err != nil {
		return nil, err
	}
	return
}
*/
